<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /*
           JSON 格式的字符串
               它和其他语言交互时的数据格式
             1.数组：
                  [
                      {
                          "id": 1,
                          "name": "xianming"
                      },
                      {
                          "id"： 2,
                          "name"： "xiaohong"
                      }
                  ]
            2.对象：
              示例：{
                     "status":0,
                     "msg":"SUCCESS",
                     "data":[
                         {
                             "id":1,
                             "name": "xiaohong"
                         },
                         {
                             "id":2,
                             "name":"xiaoming"
                         }
                     ]
              }
        */
        var obj = {
            age: 22,
            address: '郑州',
            subject: "js",
            score: 99
        }
        //    转化为 json 格式的字符串
        obj = JSON.stringify(obj);
        console.log(obj);
        console.log(typeof obj);
        sessionStorage.setItem("obj", obj);
        var result = sessionStorage.getItem("obj");
        console.log(typeof result);

        // json 格式字符串转化为对象
        result = JSON.parse(result);
        console.log(typeof result);
        console.log(result);
        console.log('-----------------------');


        var arr = [1, 22, "北京", "王一博"];
        arr = JSON.stringify(arr);
        sessionStorage.setItem('arr', arr);
        var arr2 = sessionStorage.getItem('arr');
        arr2 = JSON.parse(arr2);
        console.log(arr2);
    </script>
</body>

</html>